myChart = echarts.init(document.getElementById('gaikuangDiv'));
var path = "../";
myChart.setOption({
    title: {
        text: '男女总触发量'
    },
    tooltip: {},
    legend: {
        data: ['本阶段实际投放总次数分布']
    },
    xAxis: {
        data: []
    },

    dataZoom: [{
        type: 'slider',
        show: true,
        xAxisIndex: [0],
        left: '9%',
        bottom: -5,
        start: 10,
        end: 90 //初始化滚动条
    }],
    yAxis: {},
    series: [
        {
            name: '当天触发量',
            type: 'bar',
            data: []
        }, {
            name: '昨天触发量',
            type: 'line',
            data: []
        }
    ]
});
myChart.showLoading();

var lineList = [];
var manList = [];
var womanList = [];
var dates = [];
var data = ["男",  "女"];
var overDate;
var startDate;
var advCode;

$(function(){
//console.log(location.search.concat());
//manyValues();
    $("#gaikuangDiv").html(manyValues());
    ajaxhzx();
})
//接收多值
function manyValues(){
    var url=window.location.search;
    if(url.indexOf("?")!=-1){
        var str = url.substr(1);
        strs = str.split("&");
        var key=new Array(strs.length);
        var value=new Array(strs.length);
        for(i=0;i<strs.length;i++){
            key[i]=strs[i].split("=")[0]
            value[i]=unescape(strs[i].split("=")[1]);
            if(key[i] == "advCode"){
                advCode = value[i];
            }
            if(key[i] == "overDate"){
                overDate = value[i];
            }
            if(key[i] == "startDate"){
                startDate = value[i];
            }
        }
    }
}

function ajaxhzx() {
    //异步加载数据
    $.ajax({
        type: "post",
        async: true,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
        url: path + "rData/querylist",    //请求发送到getTig处
        data: JSON.stringify({
            advCode,startDate,overDate
        }),
        dataType: "json",        //返回数据形式为json
        contentType : "application/json",
        success: function (result) {
            //请求成功时执行该函数内容，result即为服务器返回的json对象
            if (result.rows != undefined) {
                var n = 0;
                var l = 0;
                for (var i = 0; i < result.rows.length; i++) {
                    if(result.rows[i].sex == "1"){
                        n++;
                    }else{
                        l++;
                    }
                }
                document.getElementById("men").innerText = n;
                document.getElementById("women").innerText = l;
                myChart.hideLoading();    //隐藏加载动画
                myChart.setOption({        //加载数据图表
                    xAxis: {
                        type : 'category',
                        data : ['男','女'],
                        axisTick: {
                            alignWithLabel: true
                        }
                    },
                    toolbox: {
                        show: true,
                        orient: 'horizontal',
                        showTitle: true,
                        feature: {
                            magicType: {
                                type: ['line']
                            },
                            saveAsImage: {
                                type: 'png',
                                show: true,
                                title: '保存为图片',
                            },
                            restore: {show: true}
                        }
                    },
                    series: [{
                        name:'访问量',
                        type:'bar',
                        barWidth: '20%',
                        data: [n,l]
                    }]
                });
            }else{
                alert(result.success);
                window.location.href = path + 'page/revisedData.html';
            }
        },
        error: function (errorMsg) {
            //请求失败时执行该函数
            alert("图表请求数据失败!");
            myChart.hideLoading();
        }
    });
}


// 获取base64图片
// var picBase64Info = myChart.getDataURL();
// alert(picBase64Info);
//创建form提交图片数据
var postDownLoadFile = function (options) {
    var config = $.extend(true, { method: 'post' }, options);
    var $iframe = $('<iframe id="down-file-iframe" />');
    var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
    $form.attr('action', config.url);
    //图片
    $form.append('<input type="hidden" name="base64Info" value="' + config.data + '" />');

    $iframe.append($form);
    $(document.body).append($iframe);
    $form[0].submit();
    $iframe.remove();
}
//触发下载功能
$("#download").on('click',
    function() {
        var picBase64Info = myChart.getDataURL();
        var param={};
        postDownLoadFile({
            url: path + "rData/exceloutput",
            data: picBase64Info,
            method: 'post'
        });
    });